package com.sunyur.tailor.channel.service.inner.workflow.service.impl;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.base.SunyurEventService;
import com.sunyur.tailor.channel.service.middle.workflow.MiddleWorkFlowService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author XuHui
 * @Description 审批流程入口类
 **/
public class SunyurWorkFlowEntranceServiceImpl implements SunyurEventService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurWorkFlowEntranceServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurWorkFlowEntranceServiceImpl.class);

    @Autowired
    private MiddleWorkFlowService middleWorkFlowService;

    public SunyurResult<Boolean> start(String param) {
        LOGGER_INFO.info("SunyurWorkFlowEntranceServiceImpl start param:{}", param);
        SunyurResult<Boolean> result = new SunyurResult<>(false);
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        MsgResultCode msgResultCode = JSON.parseObject(param, MsgResultCode.class);
        ChannelResult<Boolean> channelResult = middleWorkFlowService.start(msgResultCode);
        result.setErrorMessage(channelResult.getCode(), channelResult.getMessage());
        if (ChannelResultUtil.isSuccess(channelResult)) {
            result.setContent(true);
        }
        LOGGER_INFO.info("SunyurWorkFlowEntranceServiceImpl start return:{}", JSON.toJSONString(result));
        return result;
    }

    public SunyurResult<Boolean> cancel(String param) {
        LOGGER_INFO.info("SunyurWorkFlowEntranceServiceImpl cancel param:{}", param);
        SunyurResult<Boolean> result = new SunyurResult<>(false);
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        MsgResultCode msgResultCode = JSON.parseObject(param, MsgResultCode.class);
        ChannelResult<Boolean> channelResult = middleWorkFlowService.cancel(msgResultCode);
        result.setErrorMessage(channelResult.getCode(), channelResult.getMessage());
        if (ChannelResultUtil.isSuccess(channelResult)) {
            result.setContent(true);
        }
        LOGGER_INFO.info("SunyurWorkFlowEntranceServiceImpl cancel return:{}", JSON.toJSONString(result));
        return result;
    }

}
